package com.ruoyi.apiserver.base;

import com.ruoyi.apiserver.utils.JwtUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

public class MhBaseController {

    @Resource
    private JwtUtils jwtUtils;

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 设置请求分页数据
     */
//    protected void GetUserId()
//    {
//        String token = jwtUtils.getToken(request);
//        String phone = jwtUtils.getUsernameFromToken(token);
//    }

    /**
     * 获取当前用户ID
     *
     * @param request HTTP请求对象
     * @return 用户ID
     */
    protected String getUserId(HttpServletRequest request) {
        String token = jwtUtils.getToken(request);
        return jwtUtils.getUsernameFromToken(token);
    }

    /**
     * 获取当前用户ID（从当前请求中获取）
     *
     * @return 用户ID
     */
    protected Long getUserId() {
        String token = jwtUtils.getToken();
        if (token == null) {
            return  0L;
        }
        String usernameFromToken = jwtUtils.getUsernameFromToken(token);
        if (usernameFromToken == null) {
            return  0L;
        }
        return Long.parseLong(usernameFromToken);
    }
}
